$(function(){
	
	//编辑页面初始化页面
	if(opType == 'edit'){
		// 编辑页面  组织
		initEditInfoPushObj();
	}else if(opType == 'add'){
		$("#objCityId").val(-99);
		$("#PUSH_NOTE").val('');
	}

	//删除列表元素
	$('body').on({
		click:function(){
			//判断有几个元素  至少保留一个
			var len = $("#sort_div").find('.srfo').length;
			if(len > 1){
				$(this).parent('div').remove();
			}else{
				$.messager.alert('提示','请至少保留一条资讯！','info');
				return false;
			}
		}
	},'.del-row');
	
	$("#closeBtn").click(function() {
		backendCommon.closeWin("noPassWin");
	});
	$("#pushPass").click(function(){
		verifyPass();
	});
	
	$("#pushNopass").click(function(){
		backendCommon.formReset("noPassForm");
		backendCommon.openeWin('noPassWin');
		$("#saveBtn").unbind('click').click(function() {
			verifyNo();
		});
	});
	
	
	//推送申请
	$("#pushApplication").on('click',function(){
		
		if(infoFlag == false){
//			$.messager.alert('提示','待推送的资讯状态未到发布状态，请重新选择待推送的资讯！','info');
			$.messager.confirm('确认','待推送的资讯状态未到发布状态，请确认是否需要重新选择资讯推送？',function(r){    
			    if (r){    
			        window.location.href = mainDomain + '/article/queryArticleList.action?tabId=2';  
			    }    
			}); 
			return false;
		}
		//推送方式
		var pushTypeSubscribe = 0;
		if($("#PUSH_TYPE_SUBSCRIBE").attr("checked")){
			pushTypeSubscribe = 1;
		}
		var pushTypeRrt = 0;
		if($("#PUSH_TYPE_RRT").attr("checked")){
			pushTypeRrt = 1;
		}
		var  pushTypeMsg = 0;
		if($("#PUSH_TYPE_MSG").attr("checked")){
			pushTypeMsg = 1;
		}
		
		if(pushTypeSubscribe == 0 && pushTypeRrt == 0 && pushTypeMsg == 0){
			$.messager.alert('提示','请至少选择一种推送方式！','info');
			return false;
		}
		
		//推送语
		var pushNote =$.trim( $("#PUSH_NOTE").val());

		//城市
		var  objCityId =  $("#objCityId").val();
		if(objCityId == -99){
			$.messager.alert('提示','请选择推送对象所在区域城市！','info');
			return false;
		}
		//判断是否是全省
		var cityObj = [];
		var countyObj = [];
		if(objCityId == -1){
			$("div a.checkAllOrg").find(".fa").each(function(){
				if($(this).hasClass('fa-check-square-o')  &&  $(this).attr("data-id")){
					var pushObj = {};
					pushObj.objType = 1;
					pushObj.objProvinceId = 1;
					pushObj.objCityId = $(this).attr("data-id");
					cityObj.push(pushObj);
				}
			});
			if(cityObj.length == 0){
				$.messager.alert('提示','请选择推送对象所在区域！','info');
				return false;
			}
		}else{
			//区县
			$("div a.checkAllOrg").find(".fa").each(function(){
				if($(this).hasClass('fa-check-square-o')  &&  $(this).attr("data-id")){
					var pushObj = {};
					pushObj.objType = 1;
					pushObj.objProvinceId = 1;
					pushObj.objCityId = objCityId;
					pushObj.objCountyId = $(this).attr("data-id");
					countyObj.push(pushObj);
				}
			});
			if(countyObj.length == 0){
				$.messager.alert('提示','请选择推送对象所在区域！','info');
				return false;
			}
		}
		//角色
		
		var roleCodeObj = [];
		$("div a.role").find('.fa').each(function(){
			if($(this).hasClass('fa-check-square-o') &&  $(this).attr("data-code")){
				var pushObj = {};
				pushObj.objType = 2;
				pushObj.objProvinceId = 1;
				if(objCityId != -1){
					pushObj.objCityId = objCityId;
				}
				pushObj.objRoleCode = $(this).attr("data-code");
				roleCodeObj.push(pushObj);
			}
		});
		if(roleCodeObj.length == 0){
			$.messager.alert('提示','请选择推送对象所属角色！','info');
			return false;
		}
		//年级
		var gradeObj = [];
		$("div a.grade").find('.fa').each(function(){
			if($(this).hasClass('fa-check-square-o') &&  $(this).attr("data-phaseType")){
				var pushObj = {};
				pushObj.objType = 3;
				pushObj.objProvinceId = 1;
				if(objCityId != -1){
					pushObj.objCityId = objCityId;
				}
				pushObj.objPhase = $(this).attr("data-phaseType");
				pushObj.objGrade = $(this).attr("data-gradeid");
				gradeObj.push(pushObj);
			}
		});
		
		if(gradeObj.length == 0){
			$.messager.alert('提示','请选择推送对象年级！','info');
			return false;
		}
		var infoPushTemp = [];
		if(objCityId == -1){//全省   推送
			infoPushTemp = $.merge(cityObj,roleCodeObj);
		}else{ // 某个市推送
			infoPushTemp = $.merge(countyObj,roleCodeObj);
		}
		var infoPushObj = $.merge(infoPushTemp,gradeObj);
		
		//排序
		var infoDetailObj = [];
		$("#sort_div input.sort_input").each(function(){
			var infoDetail = {};
			if($(this).val() == ''){
				infoDetail.infoSort = 0;
			}else{
				infoDetail.infoSort = $(this).val();
			}
			infoDetail.infoId = $(this).attr('data-id');
			infoDetailObj.push(infoDetail);
		});
		
		//推送时间
		var pushType;
		var obj = document.getElementsByName("subType");
		$("input[name='subType']:radio").each(function () { 
			if ($(this).attr("checked")) { 
				pushType = $(this).attr('value');
			} 
		});
		
		if(pushType == 2){//预约
			var appointmentTime = $("#APPOINTMENT_TIME").val();
			if(appointmentTime == ''){
				$.messager.alert('提示','请选择预约推送时间！','info');
				return false;
			}
		}
		var infoPushAppDto = {};
		infoPushAppDto['pushTypeSubscribe']=pushTypeSubscribe;
		infoPushAppDto['pushTypeRrt']=pushTypeRrt;
		infoPushAppDto['pushTypeMsg']=pushTypeMsg;
		infoPushAppDto['pushType']=pushType;
		infoPushAppDto['pushNote']=pushNote;
		infoPushAppDto['appointmentTimeStr']=appointmentTime;
		infoPushAppDto['detailList'] = infoDetailObj;
		infoPushAppDto['objectList'] = infoPushObj;
		
		
		if(opType == 'edit'){
			infoPushAppDto['id'] = id;
			var params = {'infoPushAppDto':JSON.stringify(infoPushAppDto)};
			$.post( mainDomain + "/info/editInfoPushApplication.action" , params , function(data){
				if(data.success){
					$.messager.alert('提示','新增推送申请成功！','info');
					window.location.href = mainDomain + "/info/infoPushVerify.action";
				}else{
					$.messager.alert('提示',data.message,'info');
					return false;
				}
			});
		}else{
			var params = {'infoPushAppDto':JSON.stringify(infoPushAppDto)};
			$.post( mainDomain + "/info/infoPushToApplication.action" , params , function(data){
				if(data.success){
					$.messager.alert('提示','新增推送申请成功！','info');
					window.location.href = mainDomain + "/info/infoPushVerify.action";
				}else{
					$.messager.alert('提示',data.message,'info');
					
					return false;
				}
			});
		}
	});
	
	
	//取消
	
	$("#cancelApplication").click(function(){
		backendCommon.closeInTabs('推送资讯');
	});
	
	//资讯 摘要
	$("#copyToi").click(function(){
		if(infoFlag == false){
			$.messager.alert('提示','待推送的资讯状态未到发布状态，无法复制资讯摘要，请重新选择资讯！','info');
			return false;
		}
		var str = $.trim($("#infoNote_0").html());
		if(str != ''){
			str = str.replace(/\r/g,"").replace(/\n/g,"").replace(" ","");
		}
		$("#PUSH_NOTE").val(str);
	});
	
	
	//获取区县     如果是全省  默认选中所有
	$("#objCityId").bind('change',function(){
		var orgId = $(this).val();
		if(orgId == -1){
			orgId = 1;//江苏省  全省
		}
		var param = {"parentId":orgId};
		var opt = '';
		$.post(mainDomain + '/info/findEduOrgList.action' , param , function(result){
			$.each(result.data.data ,function(index , org ){
				opt +='<div class="radio-sec fn-left check"><a href="javascript:;" class="checkAllOrg"><i class="fa fa-square-o fa-lg" data-id="'+org.id+'"></i><span>'+org.orgName+'</span></a></div>';
			});
			$("#county_lable").html(opt);
		});
	});
	//区县全选
	$("#checkAllOrg a").click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		//获取所有的fa
		checkFaSquare($("div a.checkAllOrg"),flag);
	});
	
	//选中checkbox
	function checkFaSquare(_this, flag){
		var fa = _this.find('.fa');
		if (flag) {
			fa.addClass('fa-check-square-o').removeClass('fa-square-o');
		} else {
			fa.addClass('fa-square-o').removeClass('fa-check-square-o');
		}
	}
	// 选中
	$('body').on({
		click:function(){
			var flag = $(this).find('.fa').hasClass('fa-square-o');
			checkFaSquare($(this), flag);
		}
	},'div.check a');
	
	//角色全选
	$("#checkCode a").click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		//获取所有的fa
		checkFaSquare($("div a.role"),flag);
	});
	
	//年级   全选
	$("#checkAll a").click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		//获取所有的fa
		checkFaSquare($("div a.grade"),flag);
	});
	//小学   初中  高中  横向全选
	$("#primaryschool a, #juniorhighschool a,  #highSchool a").click(function(){
		var flag = $(this).find('.fa').hasClass('fa-square-o');
		//获取所有的fa
		//获取对应的年纪
		var cl = $(this).attr('data-id');
		var fa = $("div a.grade").find('.'+cl);
		if (flag) {
			fa.addClass('fa-check-square-o').removeClass('fa-square-o');
		} else {
			fa.addClass('fa-square-o').removeClass('fa-check-square-o');
		}
	});
	
	//初始化  编辑   页面
	function initEditInfoPushObj(){
		var params = {"id" : id};
		$.post(mainDomain + '/info/initEditInfoPushApplication.action' , params , function(data){
			var app = data.data.appVo;
			if(app.pushTypeSubscribe == 1){
				$("#PUSH_TYPE_SUBSCRIBE").attr('checked','checked');
			}
			if(app.pushTypeRrt == 1){
				$("#PUSH_TYPE_RRT").attr('checked','checked');
			}
			if(app.pushTypeMsg == 1){
				$("#PUSH_TYPE_MSG").attr('checked','checked');
			}
			//
			$("#PUSH_NOTE").val(app.pushNote);
			
			
			$("#objCityId").val(app.objCityId);
			//初始化  区县
			
			var objCityId = app.objCityId;
			var objOrgFlag = false;
			if(objCityId == -1){
				objCityId = 1;
				objOrgFlag = true;
			}
			var param = {"parentId":objCityId};
			var opt1 = '';
			$.post(mainDomain + '/info/findEduOrgList.action' , param , function(result){
				var objOrg = result.data.data;
				if(objOrg.length > 0){
					$.each(result.data.data ,function(index , org ){
						if(objOrgFlag){
							opt1 +='<div class="radio-sec fn-left check"><a href="javascript:;" class="checkAllOrg"><i class="fa fa-check-square-o fa-lg" data-id="'+org.id+'"></i><span>'+org.orgName+'</span></a></div>';
						}else{
							opt1 +='<div class="radio-sec fn-left check"><a href="javascript:;" class="checkAllOrg"><i class="fa fa-square-o fa-lg" data-id="'+org.id+'"></i><span>'+org.orgName+'</span></a></div>';
						}
					});
					$("#county_lable").html(opt1);
					var orgObjList = app.objOrgVoList;
					var orgfa = $("div a.checkAllOrg").find('.fa');
					$.each(orgObjList , function(index, org){
						$.each(orgfa , function(index, value){
							var orgid = $(this).attr('data-id');
							if(orgid == org.objCountyId){
								$(this).addClass('fa-check-square-o').removeClass('fa-square-o');
							}
						});
					});
					
					if(orgObjList.length == orgfa.length - 1){
						$('#checkAllOrg a.checkAllOrg').find('.fa').addClass('fa-check-square-o').removeClass('fa-square-o');
					}
				
				}
			});
			//
			
			//角色
			var roleObjList = app.objRoleVoList;
			var rolefa = $("div a.role").find('.fa');
			$.each(roleObjList , function(index, role){
				$.each(rolefa , function(index, value){
					var code = $(this).attr('data-code');
					if(code == role.objRoleCode){
						$(this).addClass('fa-check-square-o').removeClass('fa-square-o');
					}
				});
			});
			if(roleObjList.length == rolefa.length - 1){
				$('#checkCode a.role').find('.fa').addClass('fa-check-square-o').removeClass('fa-square-o');
			}
			
			
			//年级
			var gradeObjList = app.objGradeVoList;
			if(gradeObjList!=null && gradeObjList.length > 0 ){
				$.each(gradeObjList , function(index, grade){
					var fa = $("div a.grade").find('.fa');
					$.each(fa , function(index, value){
						var phaseType = $(this).attr('data-phaseType');
						var gradeId = $(this).attr('data-gradeid');
						if(phaseType == grade.objPhase && gradeId == grade.objGrade){
							$(this).addClass('fa-check-square-o').removeClass('fa-square-o');
						}
					});
				});
				if(gradeObjList.length == 12){//判断是否已全选
					var fa = $("div a.grade").find('.school');
					$.each(fa , function(index, value){
						$(this).addClass('fa-check-square-o').removeClass('fa-square-o');
					});
				}
			}
			//详情
			var detailList = app.detailVoList;
			if(detailList!=null && detailList.length > 0){
				var opt = '';
				$.each(detailList , function(index , detail){
					opt +='<div class="sort-row   srfo">'+
					'<input type="text" class="sort_input" value="'+detail.infoSort+'" data-id="'+detail.id+'" />'+
					'<span class="row" style="line-height: 4;">'+detail.infoTitle+'</span>'+
					'<img class="row" src="'+staticDomain+detail.infoUrl+'"></img>'+
					'<input type="button" value="删除" class="del-row row"/></div>'+
					'<div style="display:none;" id="infoNote_'+index+'">'+detail.infoDetailVo.infoNote+'</div>';
				});
				if(opt == ''){
					opt = '<span style="color:red;">所选的资讯状态不是发布状态</span>';
				}
				$("#edit_div").html(opt);
			}
			if(app.pushType == 1){
				$("#sub-immediately").attr('checked','checked');
				$("#sub-later").removeAttr('checked');
				$("#APPOINTMENT_TIME").val('');
			}else{
				$("#sub-immediately").removeAttr('checked');
				$("#sub-later").attr('checked','checked');
				$("#APPOINTMENT_TIME").val(app.appointmentTime);
			}
		});
	}
	
	function verifyPass(){
		var appid = $("#appId").val();
		var param = {"id":appid,'pushStatis':1};
		$.post(mainDomain+'/info/infoPushVerifyPass.action', param, function(result){
			if (result.success && result.state == 1) {
				$.messager.alert('提示','审核推送成功', 'info');
			} else {
				$.messager.alert('提示', result.message, 'error');
			}
			window.close();
			window.opener.location.reload();
		});
	 }
	
	function verifyNo(){
		var id = $('#appId').val();
		var remark = $('#remark').val().trim();
		if(remark == ''){
			$.messager.alert('提示', '回复意见不能为空', 'error');
			return false;
		}
		if(remark.length > 250){
			$.messager.alert('提示', '回复意见不能操作250个长度', 'error');
			return false;
		}
		var param = {
				'id':id,
				'remark':remark,
				'pushStatis':2
		};
		$.post(mainDomain+'/info/infoPushVerifyNoPass.action', param, function(result){
			if (result.success && result.state == 1) {
				$.messager.alert('提示','操作成功', 'info');
				backendCommon.closeWin('noPassWin');
			} else {
				$.messager.alert('提示', result.message, 'error');
			}
			window.close();
			window.opener.location.reload();
		});
	}
});

function intoInfoDetail(id){
	window.open(mainDomain+'/article/infoDetailPage/'+id+'.action',"资讯详情",
		"height=620, width=950,toolbar =no, menubar=no, scrollbars=yes, resizable=no, location=no, status=no");
}